#!/usr/bin/python

"""
- Read sblab db to see which services don't have an associated fastqfile
- For these services, try to download the associated fastq files from CRI-lims
  (getFilesForLibrary.py)
- For newly downloaded files:
  - Convert solexa to Sanger encoding
  - Demultiplex as required
  - Upload file stats to db
"""

import psycopg2
import sys
import os
import subprocess

FASTQ_REPOSITORY_HOST= '$uk_cri_lsrv10' ## Host where downloaded fastqfiles will go
FASTQ_REPOSITORY_DIR= '/data01/sblab/users/berald01/repository/original/fastq/'

def get_psycopgpass():
    """
    Read file ~/.psycopgpass to get the connection string to pass to
    psycopg2.connect()
    """
    conn= open(os.path.join(os.getenv("HOME"), '.psycopgpass'))
    conn= conn.readlines()
    conn_args= [x.strip() for x in conn if x.strip() != '' and x.strip().startswith('#') is False][0]
    return(conn_args)

# ----------------------[Which services need fastq]-------------------------------------------

conn= psycopg2.connect(get_psycopgpass())
cur= conn.cursor()
cur.execute("SELECT DISTINCT service_id FROM view_fastq_for_library WHERE fastqfile = 'No fastq file found' ORDER BY service_id")
service_cue= cur.fetchall()
cur.close()
sys.exit()

